package leetcode;

/**
 * 题目：验证回文串
 * 链接：https://leetcode.cn/problems/valid-palindrome/description/
 */
public class IsPalindrome {
    public static void main(String[] args) {
        String s = "A man, a plan, a canal: Panama";
        System.out.println(isPalindrome(s));
    }

    public static boolean isPalindrome(String s) {
        String news = s.toLowerCase().trim();
        if (news.isEmpty()) {
            return true;
        }
        int start = 0, end = news.length() - 1;
        // 双指针
        while (start < end) {
            System.out.println("start:" + news.charAt(start) + ",end:" + news.charAt(end));
            if (!Character.isLetterOrDigit(news.charAt(start)) || !Character.isLetterOrDigit(news.charAt(end))) {
                if (!Character.isLetterOrDigit(news.charAt(start))) {
                    start++;
                } else {
                    end--;
                }
            } else if (news.charAt(start) != news.charAt(end)) {
                return false;
            } else {
                start++;
                end--;
            }
        }
        return true;
    }
}
